package com.sunday.aomicaichang.adpter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.widgets.banner.ConvenientBanner;
import com.sunday.common.widgets.banner.holder.ViewHolderCreator;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.base.BaseApp;
import com.sunday.aomicaichang.model.Find;
import com.sunday.aomicaichang.ui.common.WebViewActivity;
import com.sunday.aomicaichang.ui.product.ProductActivity;
import com.sunday.aomicaichang.ui.product.SearchProductListActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/6/4.
 */

public class FindAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<Find> finds;

    public FindAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setmData(List<Find> finds) {
        this.finds = finds;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 1) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_banner, parent, false);
            return new TopViewHolder(v);
        }else {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_finds, parent, false);
            return new ItemViewHolder(v);
        }
    }
    @Override
    public int getItemViewType(int position) {
        return finds.get(position).getType();
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof TopViewHolder){
            final List<Find.AdvertisementsBean> findss = finds.get(position).getAdvertisements();
            TopViewHolder viewHolder1 = (TopViewHolder) holder;
            viewHolder1.banner.setPages(viewHolder1.viewHolderCreator, findss)
                    .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT)
                    .setPageIndicator(new int[]{R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused});
            viewHolder1.banner.notifyDataSetChanged();
        } else {
            final List<Find.AdvertisementsBean> findd = finds.get(position).getAdvertisements();
            ItemViewHolder viewHolder = (ItemViewHolder) holder;
            viewHolder.item.removeAllViews();
            for (final Find.AdvertisementsBean find : findd) {
                View v1 = LayoutInflater.from(mContext).inflate(R.layout.item_find, null);
                ImageView img = (ImageView) v1.findViewById(R.id.img);
                TextView tvName = (TextView) v1.findViewById(R.id.tv_name);
                TextView tvSub = (TextView) v1.findViewById(R.id.tv_sub);
                TextView tvTime = (TextView) v1.findViewById(R.id.tv_time);
                RelativeLayout itemLayout= (RelativeLayout) v1.findViewById(R.id.item);
                if (find.getName() != null)
                    tvName.setText(find.getName());
                if (find.getViceName() != null)
                    tvSub.setText(find.getViceName());
                if (find.getCreateDate() != null)
                    tvTime.setText(find.getCreateDate());
                if (!TextUtils.isEmpty(find.getImage())) {
                    Picasso.with(mContext)
                            .load(find.getImage())
                            .error(R.mipmap.ic_product)
                            .resize(150, 150).into(img);
                }
                itemLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = null;
                        switch (find.getPageType()) {
                            case 0://打开url
                                intent = new Intent(mContext, WebViewActivity.class);
                                intent.putExtra("title", "资讯详情");
                                long memberId = BaseApp.getInstance().getMember().getId();
                                intent.putExtra("url", find.getUrl()+"&memberId="+memberId);
                                break;
                            case 1://打开商品详情
                                intent = new Intent(mContext, ProductActivity.class);
                                intent.putExtra("id", Long.valueOf(find.getPageId()));
                                intent.putExtra("name", find.getName());
                                break;
                            case 2://打开商品列表
                                intent = new Intent(mContext, SearchProductListActivity.class);
                                intent.putExtra("catId", find.getPageId());
                                break;
                            case 3://打开商户详情
                                break;
                        }
                        mContext.startActivity(intent);
                    }
                });
                viewHolder.item.addView(v1);
            }
        }
    }

    @Override
    public int getItemCount() {
        return finds == null ? 0 : finds.size();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.item)
        LinearLayout item;
        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            RecyclerView.LayoutParams layoutParams=new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            item.setLayoutParams(layoutParams);
        }
    }
    public class TopViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.banner)
        ConvenientBanner banner;
        private ViewHolderCreator viewHolderCreator;
        private FindBannerHolder bannerViewHolder;
        public TopViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            banner.startTurning(3000);
            bannerViewHolder = new FindBannerHolder();
            viewHolderCreator = new ViewHolderCreator<FindBannerHolder>() {

                @Override
                public FindBannerHolder createHolder() {
                    return bannerViewHolder;
                }
            };
            bannerViewHolder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Find.AdvertisementsBean item = (Find.AdvertisementsBean) v.getTag();
                    Intent intent = null;
                    switch (item.getPageType()) {
                        case 0://打开url
                            intent = new Intent(mContext, WebViewActivity.class);
                            long memberId = BaseApp.getInstance().getMember().getId();
                            intent.putExtra("url", item.getUrl()+"&memberId="+memberId);
                            intent.putExtra("title", "资讯详情");
//                            intent.putExtra("time", item.getCreateDate());
//                            intent.putExtra("img", item.getImage());
//                            intent.putExtra("viceName", item.getViceName());
//                            intent.putExtra("id", item.getId());
                            break;
                        case 1://打开商品详情
                            intent = new Intent(mContext, ProductActivity.class);
                            intent.putExtra("id", item.getId());
                            intent.putExtra("name", item.getName());
                            break;
                        case 2://打开商品列表
                            intent = new Intent(mContext, SearchProductListActivity.class);
                            intent.putExtra("catId", item.getPageId());
                            break;
                        case 3://打开商户详情
                            break;
                    }
                    mContext.startActivity(intent);
                }
            });
        }

    }
}
